package pl.edu.agh.jemo.gui.chart;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class SinglePopulationPlotter {

	public SinglePopulationPlotter(String path) throws IOException {

		
		
		String[] colors = new String[] {
				"#00CC00", "#00CCCC", "#00FFFF", "#0033FF", "#333300",
				"#333366", "#669933", "#CCFF00", "#CC3399", "#CC6600", 
				"#CCFF99", "#CCCCFF", "#660099", "#666600", "#FF9900",
				"#0099CC", "#999933", "#9999CC", "#FF0066", "#0099CC"
				};
		
		File dir = new File(path);
		BufferedWriter scriptWriter = new BufferedWriter(new FileWriter(new File(path + "\\script.plot")));
		
		scriptWriter.append("set xrange [-2 : 2 ]\n");
		scriptWriter.append("set yrange [-2 : 2 ]\n");
		scriptWriter.append("set terminal png size 1280, 1024 crop\n");
		
		int i = 0;
		for (File f : dir.listFiles() ) {
			if (!f.isFile()) continue;
			if (!f.getName().endsWith(".txt")) continue;
			if (f.getName().contains("script")) continue;
			
			scriptWriter.append(String.format("set output \'g%d.png\'\n", i));
			scriptWriter.append(String.format("plot \'p%d.txt\' pointtype %d lc rgb \"%s\"\n", i,4* (i / 5)+1, colors[i % 20]));
			i++;
		}
		
		scriptWriter.close();
		
		Runtime runtime = Runtime.getRuntime();
		String[] cmd = {"gnuplot",path + "\\script.plot"};
		runtime.exec(cmd);

			
		
	}
	
}
